package com.hxy.subject.application.controller;

import com.hxy.subject.application.convert.SubjectLabelDTOConverter;
import com.hxy.subject.application.dto.AddSubjectLabelDTO;
import com.hxy.subject.application.dto.DelSubjectLabelDTO;
import com.hxy.subject.application.dto.EditSubjectLabelDTO;
import com.hxy.subject.application.dto.SubjectLabelDTO;
import com.hxy.subject.application.vo.SubjectLabelVO;
import com.hxy.subject.common.entity.Result;
import com.hxy.subject.domain.entity.SubjectCategoryBO;
import com.hxy.subject.domain.entity.SubjectLabelBO;
import com.hxy.subject.domain.service.ISubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author haoxuyang
 * @Date 2024/9/2 18:51
 */
@Slf4j
@RestController
@RequestMapping("/subject/label")
public class SubjectLabelController {

    @Resource
    private ISubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public Result<String> add(@RequestBody @Validated AddSubjectLabelDTO dto) {
        SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.subjectLabelDtoToBo(dto);
        subjectLabelDomainService.add(subjectLabelBO);
        return Result.success("操作成功");
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated EditSubjectLabelDTO dto) {
        SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.subjectLabelDtoToBo(dto);
        subjectLabelDomainService.update(subjectLabelBO);
        return Result.success("操作成功");
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody @Validated DelSubjectLabelDTO dto) {
        SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.subjectLabelDtoToBo(dto);
        subjectLabelDomainService.delete(subjectLabelBO);
        return Result.success("操作成功");
    }


    /**
     * 查询分类下的标签
     *
     * @return
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelVO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO dto) {
        SubjectLabelBO bo = SubjectLabelDTOConverter.INSTANCE.subjectLabelDtoToBo(dto);
        List<SubjectLabelBO> subjectLabelBOList = subjectLabelDomainService.queryLabelByCategoryId(bo);
        List<SubjectLabelVO> subjectCategoryVOList = SubjectLabelDTOConverter.INSTANCE.subjectLabelBoToVoList(subjectLabelBOList);
        return Result.success("查询成功", subjectCategoryVOList);
    }

}
